package com.nhim.security.util;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nhim.security.dao.GroupDAO;
import com.nhim.security.domain.Group;

@Service("groupManager")
public class GroupManager {
	
	@Autowired
	private GroupDAO groupDAO;

	@PostConstruct
	public void init() throws Exception {
	}

	@PreDestroy
	public void destroy() {
	}

	@Transactional(readOnly = true)
	public Group findById(Integer id) {

		return groupDAO.findById(id);

	}

	@Transactional(readOnly = true)
	public List<Group> findAll() {
		return groupDAO.findAll();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void save(Group entity) throws Exception {
		groupDAO.save(entity);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean delete(Integer id) throws Exception {

		Group group = groupDAO.findById(id);

		if (group != null)
		{
			groupDAO.delete(group);
			return true;
		}
		return false;

	}	

	public static GroupManager getFromApplicationContext(
			ApplicationContext ctx) {
		return (GroupManager) ctx
				.getBean("groupManager");
	}
}
